package com.zl.common;

import com.zl.exception.CustomUnauthorizedException;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TooManyListenersException;
import java.util.UUID;

/**
 * 文件上传公共类
 */
public class fileUpload {
    public static String upload(MultipartFile file, HttpServletRequest request) {
        if(file.isEmpty()){
            String realPath = request.getServletContext().getRealPath("/");
            //获取文件后缀
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            //文件名我这里使用UUID和时间组成的
            String newFileNamePrefix = UUID.randomUUID().toString().replace("-", "") + new SimpleDateFormat("yyyyMMddHHssSSS").format(new Date());
            String newFileName = newFileNamePrefix + "." + extension;
            //处理文件上传
            try {
                file.transferTo(new File(realPath, newFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{

        }

        return null;
    }

    public static void fileSize(MultipartFile file){
        if(file.isEmpty()){

        }else{
            throw new ArithmeticException();
        }
    }
}
